package com.industrial.governance.controller;

import java.util.Calendar;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.NumberUtil;
import com.industrial.common.core.domain.R;
import com.industrial.common.security.utils.SecurityUtils;
import com.industrial.governance.util.WordUtils;
import com.industrial.system.api.RemoteUserService;
import com.industrial.system.api.domain.SysUser;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.industrial.common.log.annotation.Log;
import com.industrial.common.log.enums.BusinessType;
import com.industrial.common.security.annotation.RequiresPermissions;
import com.industrial.governance.domain.DgQuantityQualityManage;
import com.industrial.governance.service.IDgQuantityQualityManageService;
import com.industrial.common.core.web.controller.BaseController;
import com.industrial.common.core.web.domain.AjaxResult;
import com.industrial.common.core.utils.poi.ExcelUtil;
import com.industrial.common.core.web.page.TableDataInfo;

/**
 * 数据质量管理Controller
 *
 * @author SangGuangCan
 * @date 2024-03-01
 */
@RestController
@RequestMapping("/quantityqualitymanage")
public class DgQuantityQualityManageController extends BaseController {
    @Autowired
    private IDgQuantityQualityManageService dgQuantityQualityManageService;

    @Autowired
    private RemoteUserService remoteUserService;

    @PostMapping("/genDataWord")
    public void genDataWord(HttpServletResponse response) {
        int currentYear = Calendar.getInstance().get(Calendar.YEAR);
        Map<String, Object> dataMap = dgQuantityQualityManageService.genDataWord(currentYear, response);
        String ftl = "tameplat/bg_template.ftl";
        WordUtils.getInstance().exportDocFile(ftl, "导出文件", dataMap, response);
    }

    /**
     * 查询数据质量管理列表
     */
    @RequiresPermissions("quantityqualitymanage:quantityqualitymanage:list")
    @GetMapping("/list")
    public TableDataInfo list(DgQuantityQualityManage params) {

        if (StringUtils.isNotBlank(params.getCity()) && !NumberUtil.isNumber(params.getCity())) {
            R<List<String>> listR = remoteUserService.queryAreaCodeByAreaName(params.getCity());
            if (listR.getCode() == R.SUCCESS) {
                if (listR.getData() != null && !listR.getData().isEmpty()) {
                    List<String> data = listR.getData();
                    String s = data.get(0);
                    params.setCity(s);
                }
            }
        }

        Set<String> sysRoles = SecurityUtils.getLoginUser().getRoles();
        SysUser sysUser = SecurityUtils.getLoginUser().getSysUser();

        if (sysUser.isAdmin()) {
            if (StringUtils.isBlank(params.getCity())) {
                params.setProvince("370000");
            } else {
                if (StringUtils.endsWith(params.getCity(), "0000")) {
                    params.setProvince(params.getCity());
                    params.setCity(null);
                } else if (StringUtils.endsWith(params.getCity(), "00")) {
                    params.setCity(params.getCity());
                } else {
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
            }
        } else {
            if (ArrayUtil.contains(sysRoles.toArray(), "x_01_011")) {
                if (StringUtils.isBlank(params.getCity())) {
                    params.setCounty(sysUser.getDistrict());
                } else {
                    params.setCounty(params.getCity());
                    params.setCity(null);
                }
                params.setCity(null);
            } else if (ArrayUtil.contains(sysRoles.toArray(), "s_01_011")) {
                if (StringUtils.isBlank(params.getCity())) {
                    params.setCity(sysUser.getCity());
                } else {
                    if (StringUtils.endsWith(params.getCity(), "0000")) {
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    } else if (StringUtils.endsWith(params.getCity(), "00")) {
                        params.setCity(params.getCity());
                    } else {
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            } else if (ArrayUtil.contains(sysRoles.toArray(), "p_01_011")) {
                if (StringUtils.isBlank(params.getCity())) {
                    params.setProvince(sysUser.getProvince());
                } else {
                    if (StringUtils.endsWith(params.getCity(), "0000")) {
                        params.setProvince(params.getCity());
                        params.setCity(null);
                    } else if (StringUtils.endsWith(params.getCity(), "00")) {
                        params.setCity(params.getCity());
                    } else {
                        params.setCounty(params.getCity());
                        params.setCity(null);
                    }
                }
            }
        }

        startPage();
        List<DgQuantityQualityManage> list = dgQuantityQualityManageService.selectDgQuantityQualityManageList(params);
        return getDataTable(list);
    }

    /**
     * 导出数据质量管理列表
     */
    @Log(title = "数据质量管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, DgQuantityQualityManage dgQuantityQualityManage) {
        List<DgQuantityQualityManage> list = dgQuantityQualityManageService.selectDgQuantityQualityManageList(dgQuantityQualityManage);
        ExcelUtil<DgQuantityQualityManage> util = new ExcelUtil<DgQuantityQualityManage>(DgQuantityQualityManage.class);
        util.exportExcel(response, list, "数据质量管理数据");
    }

    /**
     * 获取数据质量管理详细信息
     */
    @RequiresPermissions("quantityqualitymanage:quantityqualitymanage:query")
    @GetMapping(value = "/{baseId}")
    public AjaxResult getInfo(@PathVariable("baseId") String baseId) {
        return success(dgQuantityQualityManageService.selectDgQuantityQualityManageByBaseId(baseId));
    }

    /**
     * 新增数据质量管理
     */
    @RequiresPermissions("quantityqualitymanage:quantityqualitymanage:add")
    @Log(title = "数据质量管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody DgQuantityQualityManage dgQuantityQualityManage) {
        return toAjax(dgQuantityQualityManageService.insertDgQuantityQualityManage(dgQuantityQualityManage));
    }

    /**
     * 修改数据质量管理
     */
    @RequiresPermissions("quantityqualitymanage:quantityqualitymanage:edit")
    @Log(title = "数据质量管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody DgQuantityQualityManage dgQuantityQualityManage) {
        return toAjax(dgQuantityQualityManageService.updateDgQuantityQualityManage(dgQuantityQualityManage));
    }

    /**
     * 删除数据质量管理
     */
    @RequiresPermissions("quantityqualitymanage:quantityqualitymanage:remove")
    @Log(title = "数据质量管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{baseIds}")
    public AjaxResult remove(@PathVariable String[] baseIds) {
        return toAjax(dgQuantityQualityManageService.deleteDgQuantityQualityManageByBaseIds(baseIds));
    }
}
